# coding:utf-8 ^_^
# @Date   :
# @desc   : 制作词云图GUI

from ciyun import FenCi
from fenciimg import FenciImg

import tkinter as tk
from tkinter import messagebox
from tkinter.filedialog import askopenfilename


class FCgui(object):

    def __init__(self):
        self.root = tk.Tk()
        self.root.title("词云绘图")
        self.root.geometry('500x250')
        self.main()

    def main(self):
        self.canvas()
        self.root.mainloop()

    def canvas(self):
        ''' 对控件进行布局设计 '''
        two = tk.Frame(self.root)
        two.grid(row=0, column=0)

        tk.Label(two, text='停词文件的路径：').grid(row=0, column=0, sticky='e')
        self.shop_fengci = tk.StringVar(value='支持TXT文件格式')
        self.sh_ent = tk.Entry(two, width=40, textvariable=self.shop_fengci)
        self.sh_ent.grid(row=0, column=1)
        self.sh_but = tk.Button(two, text="选择文件", width=8, height=1, command=self.shopfile)
        self.sh_but.grid(row=0, column=2, padx=5)

        tk.Label(two, text='词典的路径：').grid(row=1, column=0, sticky='e')
        self.cidian_fengci = tk.StringVar(value='支持TXT文件格式')
        self.cd_ent = tk.Entry(two, width=40, textvariable=self.cidian_fengci)
        self.cd_ent.grid(row=1, column=1)
        self.cd_but = tk.Button(two, text="选择文件", width=8, height=1, command=self.cdfile)
        self.cd_but.grid(row=1, column=2, padx=5)

        one = tk.Frame(self.root)
        one.grid(row=1, column=0)

        tk.Label(one, text='需要分词的文件路径：').grid(row=0, column=0, sticky='e')
        self.str_fengci = tk.StringVar(value='支持TXT文件格式')
        self.fc_ent = tk.Entry(one, width=40, textvariable=self.str_fengci)
        self.fc_ent.grid(row=0, column=1)
        self.fc_but = tk.Button(one, text="选择文件", width=8, height=1, command=self.fcopenfile)
        self.fc_but.grid(row=0, column=2, padx=5)

        tk.Label(one, text='背景图的路径：').grid(row=1, column=0, sticky='e')
        self.img = tk.StringVar(value='支持jpg/png图片格式')
        self.imgent = tk.Entry(one, width=40, textvariable=self.img)
        self.imgent.grid(row=1, column=1)
        self.img_but = tk.Button(one, text="选择文件", width=8, height=1, command=self.imgopenfile)
        self.img_but.grid(row=1, column=2, padx=5)

        tk.Label(one, text='结果词云图名称：').grid(row=2, column=0, sticky='e')
        imgname = tk.StringVar(value='x.png或x.jpg')
        self.imgentry = tk.Entry(one, width=40, textvariable=imgname)
        self.imgentry.grid(row=2, column=1)

        # 字体颜色选择
        tk.Label(one, text="字体颜色：").grid(row=3, column=0, sticky='e')
        color_fon = tk.StringVar(value='Set2')
        self.color_menu = tk.Entry(one, width=20, textvariable=color_fon)
        self.color_menu.grid(row=3, column=1, sticky='w')
        strs = "* 参考颜色选项：https://matplotlib.org/2.0.2/users/colormaps.html"
        tk.Label(one, text=strs, font=('微软雅黑', 8), fg='red').grid(row=4, column=1, columnspan=2)

        # 按钮情况
        truebut = tk.Button(one, text='确定', width=5, relief="solid",
                            font=('微软雅黑', 12), command=self.tijiao_data)
        truebut.grid(row=5, column=0)

        falsebut = tk.Button(one, text='关闭', width=5, relief="solid",
                             font=('微软雅黑', 12), command=self.root.quit)
        falsebut.grid(row=5, column=2)


    def shopfile(self):
        ''' 选择文件路径事件  分词路径 '''
        shopfile = askopenfilename(title='选择文件', initialdir='C:\\', filetypes=[('Txt文件', '.txt')])
        self.shop_fengci.set(shopfile)
        return shopfile

    def cdfile(self):
        ''' 选择文件路径事件  自定义词典路径 '''
        cdfile = askopenfilename(title='选择文件', initialdir='C:\\', filetypes=[('Txt文件', '.txt')])
        self.cidian_fengci.set(cdfile)
        return cdfile

    def fcopenfile(self):
        ''' 选择文件路径事件  分词路径 '''
        filepath = askopenfilename(title='选择文件', initialdir='C:\\', filetypes=[('Txt文件', '.txt')])
        self.str_fengci.set(filepath)
        return filepath

    def imgopenfile(self):
        ''' 选择文件路径事件  背景图路径'''
        imgpath = askopenfilename(title='选择文件', initialdir='C:\\')
        self.img.set(imgpath)
        return imgpath

    def tijiao_data(self):
        ''' 获取数据事件，数据处理 '''
        sp_path = self.sh_ent.get()  # 停词文件路径
        cd_path = self.cd_ent.get()  # 词典文件路径
        fc_path = self.fc_ent.get()  # 分词文件路径
        img_path = self.imgent.get()  # 背景图路径
        img_name = self.imgentry.get()  # 结果图名称
        fon_color = self.color_menu.get()  # 字体颜色

        # 判断默认值情况
        if sp_path == '支持TXT文件格式' or len(sp_path) <= 1:
            print('【停词文件路径】未填写正确内容...')
            return None
        elif cd_path == '支持TXT文件格式':
            print('【词典文件路径】未填写正确内容...')
            return None
        elif fc_path == '支持TXT文件格式' or len(fc_path) <= 1:
            print('【需要分词的文件路径】未填写正确内容...')
            return None
        elif img_path == '支持jpg/png图片格式' or len(img_path) <= 1:
            print('【背景图的路径】未填写正确内容...')
            return None
        elif img_name == 'x.png或x.jpg' or len(img_name) <= 1:
            print('【结果词云图名称】未填写正确内容...')
            return None
        elif len(fon_color) <= 1:
            print('【字体颜色】未填写正确内容...')
            return None
        else:
            fc = FenCi(fc_path, sp_path, cidianPath=cd_path)
            fc.main()
            fci = FenciImg(img_path, img_name, fon_color)
            t = fci.fenci_to_image()
            if t == 'ok':
                aa = messagebox.askyesno('确认消息', '{} 词云图已生成，请确认是否关闭界面？'.format(img_name))
                if aa:  # 为真，关闭界面
                    self.root.quit()


if __name__ == '__main__':
    FCgui()

